package ATM.util;

import ATM.pojo.UserDO;

import java.sql.*;

public class UtilSql1 {
    private Connection con;
    private PreparedStatement statement;



    // 创建连接方法
    public Connection getConnection() {
//        Connection con = null;
        try {
            // 1. 加载驱动
            Class.forName("com.mysql.cj.jdbc.Driver");
            // 2. 创建连接
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/atm?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
                    "root", "19970328");
//            System.out.println("con:" + con);
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }

        return con;
    }

    public void close(Statement statement, Connection con) {
        //4.关闭连接

        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    public void close(Statement statement, Connection con, ResultSet resultSet) {
        //4.关闭连接
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        if (con != null) {
            try {
                con.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

    /**
     * 根据用户名和密码查询用户对象
     *
     * @param id
     * @param pwd
     * @return
     */
    public UserDO getByUsersNameAndPwd(String id, String pwd) {
        Connection con = getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
//        String sql = "select * from t_user where u_id='"+id+"'and u_pass_word ='"+pwd+"'";
        String sql = "select * from t_user where u_id=? and u_pass_word = ?";
        try {

            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            statement.setObject(2, pwd);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getString(1));
                user.setPassWord(rs.getString(2));
                user.setCount(rs.getDouble(3));
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(statement, con, rs);
        }


        return user;
    }


    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    public UserDO getById(String id) {
//        Connection con = getConnection();
        con =getConnection();
        PreparedStatement statement = null;
        ResultSet rs = null;
        UserDO user = null;
//        String sql = "select * from t_user where u_id='"+id+"'";
        String sql = "select * from t_user where u_id=?";
        try {
            statement = con.prepareStatement(sql);
            statement.setObject(1, id);
            rs = statement.executeQuery();
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getString(1));
                user.setPassWord(rs.getString(2));
                user.setCount(rs.getDouble(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(statement, con, rs);

        }
        return user;
    }


    /**
     * 根据用户名查询用户对象
     *
     * @param username
     * @return
     */
    public UserDO getByUsername(String username) {
        Connection con = getConnection();
        Statement statement = null;
        ResultSet rs = null;
        UserDO user = null;
        String sql = "select * from t_user where u_name='" + username + "'";
        try {
            statement = con.createStatement();
            rs = statement.executeQuery(sql);
            if (rs.next()) {
                user = new UserDO();
                user.setId(rs.getString(1));
                user.setPassWord(rs.getString(2));
                user.setCount(rs.getDouble(3));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close(statement, con, rs);
        }
        return user;
    }


    public void update(UserDO user) {
        // 1. 创建连接
//        Connection con = getConnection();
//        PreparedStatement statement = null;
        String sql = "update t_user set u_count='" + user.getCount() + "',u_pass_word=" + user.getPassWord() + " where u_id=?";

        try {
            // 2. 获取执行SQL语句对象
            statement = con.prepareStatement(sql);
            statement.setObject(1, user.getId());
            statement.executeUpdate();


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            // 4. 关闭连接

//            close(statement, con);
        }


    }




    public void close(){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }if (con!=null){
            try {
                con.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    public void setAutoCommit(boolean autoCommit){
        try {
            con.setAutoCommit(autoCommit);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void commit(){
        try {
            con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public void rollback(){
        try {
            con.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }










    public void transition(UserDO users1, UserDO users2) {

        Connection con = getConnection();
        PreparedStatement statement = null;
        String sql = "update t_user set u_count=? where u_id=?";
        try {
            con.setAutoCommit(false);
            statement = con.prepareStatement(sql);
            statement.setObject(1, users1.getCount());
            statement.setObject(2, users1.getId());
            int row = statement.executeUpdate();
            System.out.println("row:"+row);
//            int a = 100 / 0;
            statement = con.prepareStatement(sql);
            statement.setObject(1, users2.getCount());
            statement.setObject(2, users2.getId());
            int row1 = statement.executeUpdate();
            System.out.println("row1"+row1);
            con.commit();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (Exception e) {
            try {
                con.rollback();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        } finally {
            close(statement, con);
        }


    }


}


//
//
//    public void sub(double moneyOrigin, double money,String id) {
//        //1.创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            //2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "update t_user set u_count ="+(moneyOrigin - money)+" where u_id ='"+id+"'";
//            //3.执行SQL语句
//            int row = statement.executeUpdate(sql);//被影响了几行
//            System.out.println("影响的行数" + row);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            //4.关闭连接
//            close(statement, con);
//        }
//
//    }
//
//    public void psssWordModify(String password,String id) {
//        //1.创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            //2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "update t_user set u_pass_word ="+password+" where u_id ='"+id+"'";
//            //3.执行SQL语句
//           statement.executeUpdate(sql);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            //4.关闭连接
//            close(statement, con);
//        }
//
//    }
//    public void transfer(String id ,double moneyOrigin, String id1,double moneyOrigin1,double money ){
//        //1.创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            //2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "update t_user set u_count ="+(moneyOrigin - money)+" where u_id ='"+id+"'";
//            String sql1 = "update t_user set u_count ="+(moneyOrigin1 + money)+" where u_id ='"+id1+"'";
//            statement.executeUpdate(sql);
//            statement.executeUpdate(sql1);
//
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            //4.关闭连接
//            close(statement, con);
//        }
//
//    }
//
//
//
//
//
//
//    public void add(double moneyOrigin, double money,String id) {
//        //1.创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            //2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "update t_user set u_count ="+(moneyOrigin + money)+" where u_id ='"+id+"'";
//            statement.executeUpdate(sql);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            //4.关闭连接
//            close(statement, con);
//        }
//
//    }
//
//    /**
//     * 增加数据
//     */
//    public void insert(String name, String gender,int score) {
//        //1.创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            //2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "insert into t_student(s_name,s_gender,s_score) values('" + name + "','" + gender + "'," + score + ")";
//            //3.执行SQL语句
//            int row = statement.executeUpdate(sql);//被影响了几行
//            System.out.println("影响的行数" + row);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            //4.关闭连接
//            close(statement, con);
//        }
//
//    }
//
//    //查询
//    public List query() {
//        //创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        ResultSet rs = null;
//        List list = new ArrayList();
//
//        //2.获取换行SQL语句的对象
//        try {
//            statement = con.createStatement();
//            String sql = "select u_id id, u_pass_word passWord, u_count count from t_user";
//            //3.执行SQL并返回结果集
//            rs = statement.executeQuery(sql);
//            while (rs.next()) {
//                Users users = new Users();
//                users.setId(rs.getString("id"));
//                users.setPassWord(rs.getString("passWord"));
//                users.setCount(rs.getDouble("count"));
//                list.add(users);
////                atmJframe.getUsersArrayList().add(users);
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            close(statement, con, rs);
//        }
//        return  list;
////        return atmJframe.getUsersArrayList();
//    }
//
//    public void close(Statement statement, Connection con) {
//        //4.关闭连接
//
//        if (statement != null) {
//            try {
//                statement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if (con != null) {
//            try {
//                con.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//    }
//
//    public void close(Statement statement, Connection con, ResultSet resultSet) {
//        //4.关闭连接
//        if (resultSet != null) {
//            try {
//                resultSet.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if (statement != null) {
//            try {
//                statement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if (con != null) {
//            try {
//                con.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//    }
//
//    public static void main(String[] args) {
//        UtilSql1 test01 = new UtilSql1();
//        List list = test01.query();
//
//        System.out.println(list);
//
//
////        test01.getConnection();
////        test01.insert("小晓", "女", 88);
////                List stuList = test01.query();
////        System.out.print(stuList);
//    }
//}

//
//
//
//package com.lovo;
//
//        import java.sql.*;
//
//public class DBUtil {
//    public Connection getConnection(){
//        Connection con = null;
//        try {
//            // 1. 加载驱动
//            Class.forName("com.mysql.cj.jdbc.Driver");
//            // 2. 创建连接
//            con = DriverManager.getConnection(
//                    "jdbc:mysql://localhost:3306/j189?userSSL=true&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT",
//                    "root","lovo");
//
//        } catch (ClassNotFoundException | SQLException e) {
//            e.printStackTrace();
//        }
//        return con;
//    }
//    public void close(Connection con, Statement statement, ResultSet rs){
//        if(rs != null){
//            try {
//                rs.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if(statement != null){
//            try {
//                statement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if(con != null){
//            try {
//                con.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//    }
//
//    public void close(Connection con,Statement statement){
//        if(statement != null){
//            try {
//                statement.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//        if(con != null){
//            try {
//                con.close();
//            } catch (SQLException throwables) {
//                throwables.printStackTrace();
//            }
//        }
//    }
//
//    /**
//     * 根据用户名和密码查询用户对象
//     * @param username
//     * @param pwd
//     * @return
//     */
//    public User getByUsernameAndPwd(String username,String pwd){
//        Connection con = getConnection();
//        Statement statement = null;
//        ResultSet rs = null;
//        User user = null;
//        String sql = "select * from t_user where u_name='"+username+"' and u_pwd='"+pwd+"'";
//        try {
//            statement = con.createStatement();
//            rs = statement.executeQuery(sql);
//            if(rs.next()){
//                user = new User();
//                user.setId(rs.getInt("u_id"));
//                user.setAccount(rs.getString("u_name"));
//                user.setPwd(rs.getString("u_pwd"));
//                user.setBlance(rs.getDouble("u_blance"));
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            close(con,statement,rs);
//        }
//        return user;
//    }
//
//    /**
//     * 根据id查询
//     * @param id
//     * @return
//     */
//    public User getById(int id){
//        Connection con = getConnection();
//        Statement statement = null;
//        ResultSet rs = null;
//        User user = null;
//        String sql = "select * from t_user where u_id=" + id;
//        try {
//            statement = con.createStatement();
//            rs = statement.executeQuery(sql);
//            if(rs.next()){
//                user = new User();
//                user.setId(rs.getInt("u_id"));
//                user.setAccount(rs.getString("u_name"));
//                user.setPwd(rs.getString("u_pwd"));
//                user.setBlance(rs.getDouble("u_blance"));
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            close(con,statement,rs);
//        }
//        return user;
//    }
//
//    /**
//     * 根据用户名查询用户对象
//     * @param username
//     * @return
//     */
//    public User getByUsername(String username){
//        Connection con = getConnection();
//        Statement statement = null;
//        ResultSet rs = null;
//        User user = null;
//        String sql = "select * from t_user where u_name='"+username+"'";
//        try {
//            statement = con.createStatement();
//            rs = statement.executeQuery(sql);
//            if(rs.next()){
//                user = new User();
//                user.setId(rs.getInt("u_id"));
//                user.setAccount(rs.getString("u_name"));
//                user.setPwd(rs.getString("u_pwd"));
//                user.setBlance(rs.getDouble("u_blance"));
//            }
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            close(con,statement,rs);
//        }
//        return user;
//    }
//
//    /**
//     * 根据User对象中的数据进行修改
//     * @param user
//     */
//    public void update(User user){
//        // 1. 创建连接
//        Connection con = getConnection();
//        Statement statement = null;
//        try {
//            // 2. 获取执行SQL语句对象
//            statement = con.createStatement();
//            String sql = "update t_user set u_pwd='"+user.getPwd()+"',u_blance="+user.getBlance()+" where u_id="+user.getId();
//
//            // 3. 执行SQL语句
//            int row = statement.executeUpdate(sql);
//        } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        } finally {
//            // 4. 关闭连接
//            close(con,statement);
//        }
//    }
//
//
//}